package com.alibaba.user.controller;

import com.alibaba.basic.dto.BinderDto;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.user.dto.LoginDto;
import com.alibaba.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){

        try {
            Map map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后再试");
        }
    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */

    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统一场，请稍后重试");
        }
    }


    /**
     * 接口：用户绑定微信
     * @param binderDto
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderDto binderDto){
        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            return new AjaxResult(false,"系统繁忙，请稍后再试！！");
        }
    }

    /**
     * 接口：用户退出登录
     * @param token
     * @return AjaxResult 响应给前端
     */
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable String token){
        try {
           logininfoService.logout(token);
           return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"系统繁忙，请稍后再试！！");
        }
    }
}
